package com.myreminder.client.widgets;

import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.ImageCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.IdentityColumn;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.myreminder.data.Reminder;
import com.myreminder.data.User;
import com.myreminder.service.FacebookUserService;
import com.myreminder.service.FacebookUserServiceAsync;

public class ReminderTable extends Composite {
	
	private static final FacebookUserServiceAsync facebookUserService = GWT.create(FacebookUserService.class);
	
	VerticalPanel main = new VerticalPanel();
	CellTable<Reminder> reminderTable = new CellTable<Reminder>();
	User currentUser;
	
	public ReminderTable(User user){
		this.currentUser = user;
		main.add(new Image("../images/heading_active_reminder.png"));
		main.add(reminderTable);
		main.setWidth("100%");
		buildTable();
		initWidget(main);
	}
	
	public void updateTable(){
		reminderTable.setRowData(currentUser.reminder);
		reminderTable.redraw();
	}
	
	private void buildTable(){
		TextColumn<Reminder> col = new TextColumn<Reminder>() {
			@Override
			public String getValue(Reminder object) {
				// TODO Auto-generated method stub
				return object.titel;
			}
		};
		reminderTable.addColumn(col,"title");
		TextColumn<Reminder> col2 = new TextColumn<Reminder>() {
			@Override
			public String getValue(Reminder object) {
				// TODO Auto-generated method stub
				DateTimeFormat dateFormat = DateTimeFormat.getFormat("dd.MM.yyyy");
				return dateFormat.format(object.datum);
			}
		};
		reminderTable.addColumn(col2,"date");
				
		reminderTable.addColumn(
				new Column<Reminder, Reminder>(
						new ActionCell("remove",new ActionCell.Delegate<Reminder>() {
							@Override
							public void execute(Reminder object) {
								facebookUserService.removeReminder(object, new AsyncCallback<Void>() {
									@Override
									public void onFailure(
											Throwable caught) {
										GWT.log("error");
									}
									@Override
									public void onSuccess(Void result) {
										GWT.log("entfernen");
									}
								});
								currentUser.reminder.remove(object);
								updateTable();
							}
						})
				) {
					@Override
					public Reminder getValue(Reminder object) {
						// TODO Auto-generated method stub
						return object;
					}
			},"remove");
		reminderTable.setRowData(currentUser.reminder);
	}
}
